package com.qf.fmall.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall.index.entity.Category;
import com.qf.fmall.index.entity.Product;
import com.qf.fmall.index.entity.ProductImg;
import com.qf.fmall.index.mapper.CategoryMapper;
import com.qf.fmall.index.mapper.ProductImgMapper;
import com.qf.fmall.index.mapper.ProductMapper;
import com.qf.fmall.index.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author djl
 * @since 2024-04-25
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private ProductMapper productMapper;


    @Autowired
    private ProductImgMapper productImgMapper;


    @Override
    public List<Category> categoryList() {

        //1. 查询所有的一级分类信息
        // select * from category where parent_id=0;
        List<Category> level1Cats = this.list(new QueryWrapper<Category>().eq("parent_id", 0));

        //2. 给所有的一级分类，查询对应的二级分类，设置到响应的一级分离对象中
        for (Category level1Cat : level1Cats) {

            // select * from category where parent_id= level1Cat.getCategoryId ;
            List<Category> level2Cats = this.list(new QueryWrapper<Category>().eq("parent_id", level1Cat.getCategoryId()));

            //3. 查询所有二级分类对应的三级分类
            for (Category level2Cat : level2Cats) {
                List<Category> level3Cats = this.list(new QueryWrapper<Category>().eq("parent_id", level2Cat.getCategoryId()));
                level2Cat.setCategories(level3Cats);
            }

            // 把二级分类回填到一级分类中
            level1Cat.setCategories(level2Cats);
        }

        return level1Cats;
    }

    @Override
    public List<Category> categoryListWithOneSql() {

        List<Category> list = list();  //分类数据

        HashMap<Integer, Category> integerCategoryHashMap = new HashMap<>();//存分类对象,分类ID

        for (Category category : list) {
            Integer categoryId = category.getCategoryId();
            integerCategoryHashMap.put(categoryId,category);
        }


        System.out.println(integerCategoryHashMap);  //打印所有分类数据

        ArrayList<Category> objects = new ArrayList<>();

        for (Category category : list) {
            Integer parentId = category.getParentId();
            if(parentId.equals(0)){
                objects.add(category);
            }
            else{
                Category category1 = integerCategoryHashMap.get(parentId); // 获取正在处理的分类的上级分类
                List<Category> categories = category1.getCategories();
                categories.add(category);
            }
        }

        return objects;
    }

    @Override
    public List<Category> categoryRecommends() {

        //1. 查询所有一级分类信息
        // select * from category where parent_id=0;
        List<Category> categories = this.list(new QueryWrapper<Category>().eq("parent_id", 0));

        //2. 查询每个一级分类下属的商品信息，按照销量排序，取前六
        for (Category category : categories) {
            // 如何查询该大类下属的商品信息？
            // select * from product where root_category_id= category.getId order by sold_num desc limit 6

            QueryWrapper<Product> wrapper = new QueryWrapper<>();
            wrapper.eq("root_category_id",category.getCategoryId())
                            .orderByDesc("sold_num")
                                    .last("limit 6");

            List<Product> products = productMapper.selectList(wrapper);
            //3. 查询商品图片信息
            if (products!=null&&products.size()>0){
                for (Product product : products) {
                    // select * from product_img where product_id = 商品id

                    List<ProductImg> imgs =
                            productImgMapper.selectList(new QueryWrapper<ProductImg>().eq("product_id", product.getProductId()));
                    product.setImgs(imgs);

                }
            }

            category.setProducts(products);

        }

        return categories;
    }
}
